package cn.wiseda.finance.dao.util;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

import cn.wiseda.finance.dao.util.page.Page;
import cn.wiseda.finance.dao.util.page.Pager;
import cn.wiseda.finance.dao.util.page.PaginationHelper;

/**
 * 引用Spring JDBC Templete模板，供所有子类使用
 * 
 * @author wwei
 * @param <E>
 * 
 */
public abstract class JdbcBasicDao {

	private JdbcTemplate jdbcTemplate;

	@Resource
	public void setJdbcTemplate(DataSource dataSource) {
		jdbcTemplate = new JdbcTemplate(dataSource);
	}

	public JdbcTemplate getJdbc() {
		return jdbcTemplate;
	}

	/**
	 * 
	 * 增加并且获取主键
	 * 
	 * @param sql
	 *            sql语句
	 * @param params
	 *            参数
	 * @return 主键
	 */
	public int insertAndGetKey(final String sql) {
		KeyHolder keyHolder = new GeneratedKeyHolder();
		getJdbc().update(new PreparedStatementCreator() {
			public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {

				PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
				return ps;
			}
		}, keyHolder);

		int generatedId = keyHolder.getKey().intValue();
		return generatedId;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public Page fetchPage(final String sqlCountRows, final String sqlFetchRows, final int pageNo, final int pageSize,
			final ParameterizedRowMapper rowMapper, final Object... args) {
		PaginationHelper help = new PaginationHelper();
		// Page fetchPage = help.fetchPage(jdbcTemplate, sqlCountRows, sqlFetchRows, pageNo, pageSize, rowMapper, args);
		Page fetchPage = help.fetchPage(getJdbc(), sqlCountRows, sqlFetchRows, args, pageNo, pageSize, rowMapper);
		return fetchPage;
	}

	public <T> Pager<T> fetchPager(String sql, int pageNo, int pageSize, Class<T> clazz, Object... params) {
		BeanPropertyRowMapper<T> bprm = new BeanPropertyRowMapper<>(clazz);
		Pager<T> pager = new Pager<T>(sql, pageNo, pageSize, getJdbc(), bprm, params);
		return pager;
	}

	/**
	 * 通用查询方法，返回不分页的List
	 * 
	 * @param sql
	 * @param clazz
	 * @return
	 */
	public <T> List<T> queryForBeans(final String sql, Class<T> clazz, Object... params) {
		BeanPropertyRowMapper<T> bprm = new BeanPropertyRowMapper<>(clazz);
		List<T> query = getJdbc().query(sql, bprm, params);
		return query;
	}

	public <T> T queryForBean(final String sql, Class<T> clazz, Object... params) {
		BeanPropertyRowMapper<T> bprm = new BeanPropertyRowMapper<>(clazz);
		T bean = getJdbc().queryForObject(sql, bprm, params);
		return bean;
	}

	public int update(final String sql, Object... params) {
		int update = getJdbc().update(sql, params);
		return update;
	}

	public void ed() {
		getJdbc().update(new PreparedStatementCreator() {

			@Override
			public PreparedStatement createPreparedStatement(Connection con) throws SQLException {
				// TODO Auto-generated method stub
				return null;
			}
		});
	}
}
